/**
 * 
 */
package ch.comtools.servermanager.ssh.ui.view;

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.jobs.Job;
import org.eclipse.jface.dialogs.IMessageProvider;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.forms.widgets.Form;
import org.eclipse.ui.forms.widgets.FormToolkit;
import org.eclipse.ui.part.ViewPart;
import org.eclipse.ui.progress.UIJob;

import ch.comtools.jsch.Channel;
import ch.comtools.jsch.JSchException;
import ch.comtools.servermanager.ssh.SSH;
import ch.comtools.servermanager.ssh.core.SecureChannelFactory;
import ch.comtools.servermanager.ssh.core.SessionInfo;
import ch.comtools.servermanager.ssh.ui.terminal.SecureShellTerminal;

/**
 * @author Roger Dudler <roger.dudler@gmail.com>
 * @version $Id: ConsoleView.java 56 2007-04-19 15:00:55Z roger.dudler $
 */
public class ConsoleView extends ViewPart {

	private SecureShellTerminal terminal;
	private Form form;

	/* (non-Javadoc)
	 * @see org.eclipse.ui.part.WorkbenchPart#createPartControl(org.eclipse.swt.widgets.Composite)
	 */
	@Override
	public void createPartControl(Composite parent) {
		GridLayout layout = new GridLayout();
		GridData layoutData;
		layout.marginHeight = layout.marginWidth = 0;
		layout.verticalSpacing = 0;
		parent.setLayout(layout);
		
		// add form header
		FormToolkit toolkit = new FormToolkit(parent.getDisplay());
		form = toolkit.createForm(parent);
		toolkit.decorateFormHeading(form);
		layoutData = new GridData(GridData.FILL_BOTH);
		form.setLayoutData(layoutData);
		layout = new GridLayout();
		layout.marginHeight = layout.marginWidth = 1;
		form.getBody().setLayout(layout);
		
		// add terminal
		this.terminal = new SecureShellTerminal(form.getBody());
		layoutData = new GridData(GridData.FILL_BOTH);
		this.terminal.getControl().setLayoutData(layoutData);
	}
	
	/* (non-Javadoc)
	 * @see org.eclipse.ui.part.WorkbenchPart#setFocus()
	 */
	@Override
	public void setFocus() {
		this.terminal.getControl().setFocus();
	}

	/**
	 * @param info
	 */
	public void setSessionInfo(final SessionInfo info) {
		
		// set form title
		form.setText(info.getUser() + "@" + info.getHost());
		form.setMessage("Trying to connect...", IMessageProvider.NONE);
		form.setBusy(true);
		
		// update part name
		this.setPartName(info.getUser() + "@" + info.getHost());
		
		// try to connect
		Job job = new UIJob(this.getSite().getShell().getDisplay(), "Connect to SSH server") {
			public IStatus runInUIThread(IProgressMonitor monitor) {
				try {
					Channel channel = SecureChannelFactory.getInstance().createChannel(info, SSH.CHANNEL_TYPE_SHELL);
					System.out.println("[SSH] server version [" + channel.getSession().getServerVersion() + "]");
					terminal.setChannel(channel);
					channel.connect();
					form.setMessage("Connected", IMessageProvider.NONE);
					form.setBusy(false);
					return Status.OK_STATUS;
				} catch (JSchException e) {
					e.printStackTrace();
					form.setMessage("Connection failed - " + e.getLocalizedMessage(), IMessageProvider.ERROR);
					form.setBusy(false);
					return Status.CANCEL_STATUS;
				}
			}
		};
		job.schedule();
	}

}
